﻿




	using System.Collections.Generic;
    using System;
	using System.Linq;    
    using System.Data;        
	using System.ServiceModel.DomainServices.Hosting;
    using System.ServiceModel.DomainServices.Server;
using DeklaritModel;

namespace WebSilverlight.Web
{
	[EnableClientAccess()]    
    public partial class ProductDomainService : DomainService
    { 
		#region Properties						
		protected DeklaritModel.ProductDataSet ContextDataSet;
		// Setear Property (global o anivel de entidad o ambas?)
		const int PageSize = 6;
        
        public virtual IProductDataAdapter DataAdapter 
        {
            get 
            {
				if(DataAdapterFactory.Provider == null) //return null;
					DataAdapterFactory.Provider = new SimpleDataAdapterFactory();

                return DataAdapterFactory.GetProductDataAdapter(); 
            }
        }
		
		#endregion
        
        private void FillProductDataSet(DeklaritModel.ProductDataSet dsProductDataSet1, int page, Int16? ProductId )
        {   
            if (ProductId.HasValue )
            {   
				DataAdapter.Fill(dsProductDataSet1, ProductId.Value);                            
            }
            else
            {
					if (page == -1)
						DataAdapter.Fill(dsProductDataSet1);
					else DataAdapter.FillPage(dsProductDataSet1, (page-1) * PageSize, PageSize);				
            }      
        } 
        private IQueryable<ProductWrapper> GetPage(int pageNumber)
        {
            ContextDataSet = new DeklaritModel.ProductDataSet();
            FillProductDataSet(ContextDataSet, pageNumber, null );

            EnumerableRowCollection<DataRow> dr = ContextDataSet.Product.AsEnumerable();
            
			IEnumerable<ProductWrapper> query = from row in            
                            dr
                        select new ProductWrapper
                        {
							ProductId = row.Field<Int16>("ProductId"),
							ProductName = row.Field<string>("ProductName"),
							ProductType = row.Field<string>("ProductType"),
							ProductSize = row.Field<Int16>("ProductSize"),
							ProductIsFragile = row.Field<bool>("ProductIsFragile"),
							ProductClass = row.Field<Int16>("ProductClass"),
                        };            
            IQueryable<ProductWrapper> iq = query.AsQueryable();            
            return iq;            
        } 
                       
        public IQueryable<ProductWrapper> GetProductPage(int pageNumber)
        {
            return GetPage(pageNumber );
        }       
		public IQueryable<ProductWrapper> GetProduct()
        {
            return GetPage(-1 );
        }
        private void GetProduct(Int16? ProductId)
        {
			ContextDataSet = new DeklaritModel.ProductDataSet();
            FillProductDataSet(ContextDataSet, -1, ProductId.Value);
        }
		public void UpdateProduct(ProductWrapper current)        
        {			
			GetProduct(current.ProductId);

            DataRow updateRow = null;
            foreach (DataRow row in ContextDataSet.Product.Rows)
            {
                updateRow = row;                
                break;
            }

            //var Changes = this.ChangeSet.GetOriginal(current);
			//if (Changes.ProductId != current.ProductId)
                updateRow.SetField("ProductId", current.ProductId);							
			//if (Changes.ProductName != current.ProductName)
                updateRow.SetField("ProductName", current.ProductName);							
			//if (Changes.ProductType != current.ProductType)
                updateRow.SetField("ProductType", current.ProductType);							
			//if (Changes.ProductSize != current.ProductSize)
                updateRow.SetField("ProductSize", current.ProductSize);							
			//if (Changes.ProductIsFragile != current.ProductIsFragile)
                updateRow.SetField("ProductIsFragile", current.ProductIsFragile);							
			//if (Changes.ProductClass != current.ProductClass)
                updateRow.SetField("ProductClass", current.ProductClass);							
			DataAdapter.Update(ContextDataSet);    
        }        
        public void InsertProduct(ProductWrapper newObj)        
        {			
			DeklaritModel.ProductDataSet  dsProductDataSet1 = new DeklaritModel.ProductDataSet();            
			ProductDataSet.ProductRow row = dsProductDataSet1.Product.NewProductRow();
			row.ProductId  = newObj.ProductId;
			row.ProductName  = newObj.ProductName;
			row.ProductType  = newObj.ProductType;
			row.ProductSize  = newObj.ProductSize;
			row.ProductIsFragile  = newObj.ProductIsFragile;
			row.SetField("ProductClass", newObj.ProductClass);	
			//row.ProductClass  = newObj.ProductClass;
            dsProductDataSet1.Product.AddProductRow(row);
            DataAdapter.Update(dsProductDataSet1);             
        }
        public void DeleteProduct(ProductWrapper delObj)        
        {			
			DeklaritModel.ProductDataSet  dsProductDataSet1 = new DeklaritModel.ProductDataSet();            
			ProductDataSet.ProductRow row = dsProductDataSet1.Product.NewProductRow();
			row.ProductId  = delObj.ProductId;
			row.ProductName  = delObj.ProductName;
			row.ProductType  = delObj.ProductType;
			row.ProductSize  = delObj.ProductSize;
			row.ProductIsFragile  = delObj.ProductIsFragile;
			row.SetField("ProductClass", delObj.ProductClass);				
            dsProductDataSet1.Product.RemoveProductRow(row);
            DataAdapter.Update(dsProductDataSet1);             
        }
        [Invoke]
        public int ItemCount()
        {            
            return DataAdapter.GetRecordCount();
        }
	}
}